/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";

jQuery(function() {
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/expert_grid/login.html';
    }
    let changePassword = {
        init: function(){
            this.setUserName();
            $('.changepw-form').validate({
                errorElement: 'label', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                    passWd: {
                        required: true
                    },
                    nPassWd: {
                        required: true
                    },
                    rnPassWd: {
                        required: true,
                        equalTo: "#nPassWd"
                    }
                },

                messages: {
                    passWd: {
                        required: "请输入原密码"
                    },
                    password: {
                        required: "请填写密码"
                    },
                    rnPassWd: {
                        required: "请再输入一遍",
                        equalTo: "二次输入新密码不一致"
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    $('.alert-error', $('.login-form')).show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.control-group').addClass('error'); // set error class to the control group
                },

                // success: function (label) {
                //     // label.closest('.control-group').removeClass('error');
                //     // label.remove();
                // },

                errorPlacement: function (error, element) {
                    error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
                },

                submitHandler: function (form) {
                    let $form = $(form);
                    let username = $($form.find('.J_username')[0]).val();
                    let passWd = $form.find('#passWd').val();
                    let nPassWd = $form.find('#nPassWd').val();
                    var data = {
                        // "usrNam": username,
                        "psw": passWd,
                        "pswNew": nPassWd
                    };
                    console.log(data);

                    $.ajax({
                        url: HOST_ADDR+'/cpmp/expt/modPsw',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType:"application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        success: function(resp){
                            if (!resp.success) {
                                alert(resp.msgCod+' '+resp.msgInf);
                                return;
                            }
                            alert('修改密码成功');
                        },
                        error:function(err){
                            console.log(err);
                        }

                    })
                }
            });
        },
        setUserName(){
            let username = sessionStorage.getItem('username');
            $('.J_username').html(username);
        }
    }
    App.init();
    changePassword.init();
});
